package com.jic.member.controller.manage;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MememberUser;
import com.jic.member.request.MememberUserRequest;
import com.jic.member.response.MememberUserResponse;
import com.jic.member.service.MememberUserService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("meMemberUser")
@ApiOperation(value = "会员母子平台信息查询")
@Slf4j
public class MememberUserController {

    @Autowired
    private MememberUserService mememberUserService;

    /**
     *
     * @param form
     * @return
     */

    @ApiOperation(value = "查询母子平台会员列表信息")
    @ResponseBody
    @RequestMapping(value = "/queryMemberUserList")
    public RestResult<PageResult<MememberUserResponse>> queryMemberUserList(@RequestBody MememberUserRequest form) {
        log.info("查询母子平台会员列表信息=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MememberUser mememberUser = BeanUtil.copy(form, MememberUser.class);
        PageResult<MememberUser> result = mememberUserService.queryPage(mememberUser, page);
        List<MememberUserResponse> list = result.getRows().stream().map(temp -> {
            MememberUserResponse mememberUserResponse = BeanUtil.copy(temp, MememberUserResponse.class);
            return mememberUserResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MememberUserResponse> pageResult = new PageResult<>();
        pageResult.setRows(list);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

//    @ApiOperation(value = "查询母子平台会员列表信息")
//    @ResponseBody
//    @RequestMapping(value = "/queryMemberUserLists")
//    public RestResult<PageResult<MememberUserResponse>> queryMemberUserLists(@RequestBody MememberUserRequest form) {
//        log.info("查询母子平台会员列表信息=========》,{}", form.toString());
//        Page page = new Page();
//        page.setLimit(form.getLimit());
//        page.setStart(form.getStart());
//        MememberUser mememberUser = BeanUtil.copy(form, MememberUser.class);
//        PageResult<MememberUser> result = mememberUserService.queryPages(mememberUser, page);
//        List<MememberUserResponse> list = result.getRows().stream().map(temp -> {
//            MememberUserResponse mememberUserResponse = BeanUtil.copy(temp, MememberUserResponse.class);
//            return mememberUserResponse;
//        }).filter(item -> item != null).collect(Collectors.toList());
//        PageResult<MememberUserResponse> pageResult = new PageResult<>();
//        pageResult.setRows(list);
//        pageResult.setTotal(result.getTotal());
//        return RestResult.success(pageResult);
//    }


    /**
     * description: 根据用户id查询用户组所有用户
     *
     * @author: jackHe
     * @time: 2020/6/11 20:04
     */
    @ApiOperation(value = "根据用户id查询用户组所有用户")
    @ResponseBody
    @RequestMapping(value = "/queryUserGroupAllUser")
    public RestResult<PageResult<MememberUserResponse>> queryUserGroupAllUser(@RequestBody MememberUserRequest form) {
        log.info("根据用户id查询用户组所有用户=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MememberUser mememberUser = BeanUtil.copy(form, MememberUser.class);
        PageResult<MememberUser> result = mememberUserService.queryUserGroupPage(mememberUser, page);
        List<MememberUserResponse> list = result.getRows().stream().map(temp -> {
            MememberUserResponse mememberUserResponse = BeanUtil.copy(temp, MememberUserResponse.class);
            return mememberUserResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MememberUserResponse> pageResult = new PageResult<>();
        pageResult.setRows(list);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
	@ApiOperation(value = "加入黑/白名单")
    @ResponseBody
    @RequestMapping(value = "/isBlacklist")
    public RestResult isBlacklist(@RequestBody MememberUserRequest form) {
        log.info("黑名单信息=========》,{}", form.toString());
        MememberUser mememberUser = BeanUtil.copy(form, MememberUser.class);
        long val = mememberUserService.update(mememberUser);
        return RestResult.success(null);
    }

    @ApiOperation(value = "修改会员信息")
    @ResponseBody
    @RequestMapping("updateMember")
    public RestResult<Long> updateMember(MememberUserRequest request){
        Long result = mememberUserService.updateMember(request);
        if (result>0){
            return RestResult.success(result);
        }
        return RestResult.error("9999","修改异常");
    }


    @ApiOperation(value = "查询会员信息")
    @ResponseBody
    @RequestMapping(value = "/queryUserInfo")
    public RestResult queryUserInfo(@RequestBody MememberUserRequest form) {
    	RestResult restResult = new RestResult();
        log.info("黑名单信息=========》,{}", form.toString());
        MememberUser mememberUser = mememberUserService.queryByPrimaryKey(form.getId());
        if(null != mememberUser) {
        	restResult.setCode("0000");
        	restResult.setData(mememberUser);
        }
        return restResult;
    }

    @ApiOperation(value = "查询会员列表表头信息")
    @ResponseBody
    @RequestMapping(value = "/queryUserHeadInfo")
    public RestResult queryUserHeadInfo(@RequestBody MememberUserRequest form) {
        return RestResult.success(mememberUserService.queryUserHeadInfo(form));
    }

    @ApiOperation(value = "手机模糊查询用户信息")
    @ResponseBody
    @RequestMapping(value = "/queryUserInfoByPhone")
    public RestResult<List<MememberUserResponse>> queryUserInfoByPhone(@RequestBody MememberUserRequest form) {
        MememberUser user = new MememberUser();
        user.setPhone(form.getPhone());
        user.setDeptId(form.getAdminOperate().getDeptId());
        return mememberUserService.queryUserInfoByPhone(user);
    }



    @ApiOperation(value = "修改用户状态")
    @ResponseBody
    @RequestMapping(value = "/updateStatus")
    public RestResult<Boolean> update(@RequestBody MememberUserRequest form) {
        Boolean value= mememberUserService.updateStatus(form);
        return RestResult.success(value);
    }


    @ApiOperation(value = "总条数")
    @ResponseBody
    @PostMapping("/count")
    public RestResult<List<MememberUserResponse>> count(){
        List<MememberUserResponse> list= mememberUserService.count();
        if (list==null) {
            return RestResult.error("6666","查询总条数失败");
        }
        return RestResult.success(list);
    }




}
